package com.classtest.classtestdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.classtest.classtestdemo.bean.DbCartitem;
import com.classtest.classtestdemo.bean.DbGoods;
import com.classtest.classtestdemo.bean.DbUser;
import com.classtest.classtestdemo.service.DbCartitemService;
import com.classtest.classtestdemo.service.DbGoodsService;
import com.classtest.classtestdemo.utlis.PKGeneration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cartitem")
public class DbCartitemController {
    @Autowired
    DbCartitemService dbCartitemService;

    @RequestMapping("/addCartitem")
    public String addCartitem(@RequestBody String json, HttpSession session){
        Map<String,Object> result = new HashMap<>();
        try{
            DbUser user = (DbUser) session.getAttribute("user");
            if(user != null){
                DbCartitem dbCartitem = JSONObject.parseObject(json,DbCartitem.class);
                dbCartitem.setCartItemId(PKGeneration.INSTANCE.nextId());
                dbCartitem.setBuyerId(user.getId());
                dbCartitemService.addCartitem(dbCartitem);
                result.put("reFlag","ok");
            }else {
                result.put("reFlag","error");
                result.put("msg","没有登录");
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        return JSON.toJSONString(result);
    }

    @Autowired
    DbGoodsService dbGoodsService;
    @RequestMapping("/selectAllByUsId")
    public String selectByUsId(HttpSession session){
        Map<String,Object> result = new HashMap<>();
        try{
            DbUser user = (DbUser) session.getAttribute("user");
            if(user != null){
                DbCartitem dbCartitem = new DbCartitem();
                dbCartitem.setBuyerId(user.getId());

                List<DbCartitem> dbCartitems = dbCartitemService.selectByUsId(dbCartitem);

                List<Object> list = new ArrayList<>();
                for (DbCartitem db:dbCartitems
                     ) {
                    Map<String,Object> map = new HashMap<>();
                    List<DbGoods> dbGoods = dbGoodsService.selectByGsId(db.getGsId());
                    DbGoods dbGoods1 = new DbGoods();
                    dbGoods1 = dbGoods.get(0);

                    map.put("name",dbGoods1.getGsName());
                    map.put("price",db.getPrice());
                    map.put("cartItemId",db.getCartItemId());
                    map.put("gsId",db.getGsId());
                    map.put("usId",db.getUsId());
                    map.put("quantity",db.getQuantity());

                    list.add(map);
                }
                result.put("reFlag","ok");
                result.put("cartitems",list);
            }else{
                result.put("reFlag","error");
                result.put("msg","没有登录");
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        return JSON.toJSONString(result);
    }

    @RequestMapping("/deleteByCartItemId")
    public String deleteByCartItemId(@RequestBody String json){
        Map<String,Object> result = new HashMap<>();
        try{
            DbCartitem dbCartitem = JSONObject.parseObject(json,DbCartitem.class);
            dbCartitemService.deleteByCartItemId(dbCartitem.getCartItemId());
            result.put("reFlag","ok");
        }catch (Exception e){
            result.put("reFlag","error");
            result.put("error",e.toString());
        }
        return JSON.toJSONString(result);
    }
}
